<?php
/**
 * 新闻信息操作类
 * @author lifw
 */
Class IOSS_News {
	const STATE_ENABLE = 1;
	private static $_instance;
	
	public static function getInstance(){
		if(self::$_instance == null){
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	/**
	 * 获取文章详细
	 * @param int $id
	 * @return object
	 */
	public function getById($id){
		$cache = IOSS_Cache::getInstance();
		$key = __METHOD__.'('.$id.')';
		if($datas = $cache->get($key)){
			return $datas;
		}
		$db = IOSS_DB::getInstance();
		$db->select('cms_node.*, cms_model.code as model_code');
		$db->from('cms_node');
		$db->join('cms_category', 'cms_node.category_id=cms_category.id', 'left');
		$db->join('cms_model', 'cms_category.model_id=cms_model.id', 'left');
		$db->where('cms_node.id', $id);
		$db->where('cms_node.status', self::STATE_ENABLE);
		if(!($row = $db->get()->row())){
			return null;
		}
		$row = self::ext($row, $db);
		$cache->set($key, $row);
		return $row;
	}
	
	/**
	 * 使用code获取文章详细
	 * @param string $cat_code  目录code
	 * @param string $node_code 文章code
	 * @return object
	 */
	public function getByCode($cat_code, $node_code){
		$cache = IOSS_Cache::getInstance();
		$key = __METHOD__.'('. implode(' , ' , func_get_args()) .')';
		if($datas = $cache->get($key)){
			return $datas;
		}
		$db = IOSS_DB::getInstance();
		$db->select('cms_node.*, cms_model.code as model_code');
		$db->from('cms_node');
		$db->join('cms_category', 'cms_node.category_id=cms_category.id', 'left');
		$db->join('cms_model', 'cms_category.model_id=cms_model.id', 'left');
		$db->where('cms_category.code', $cat_code);
		$db->where('cms_node.code', $node_code);
		$db->where('cms_node.status', self::STATE_ENABLE);
		if(!($row = $db->get()->row())){
			return null;
		}
		$row = self::ext($row, $db);
		$cache->set($key, $row);
		return $row;
	}
	
	/**
	 * 按指定目录代码，查找数据列表
	 * @param string $cat_code 目录代码
	 * @param integer $limit 限制数量
	 * @return array
	 */
	public function getList($cat_code, $limit = '', $offset = ''){
		$cache = IOSS_Cache::getInstance();
		$key = __METHOD__.'('. implode(',' , func_get_args()) .')';
		if(($datas = $cache->get($key)) !== false){
			return $datas;
		}
		$c = $this->getCategoryInfo($cat_code);
		if(!$c){
			return array();
		}
		$db = IOSS_DB::getInstance();
		$table_name = 'cms_node_' . $c->model;
		$db->select('cms_node.*,' . 'm.*');
		$db->from('cms_node');
		$db->join($table_name . ' as m' , ' m.node_id=cms_node.id', 'left');
		$db->where('cms_node.category_id', $c->id);
		$db->where('cms_node.status', self::STATE_ENABLE);
		$db->order_by('publish_time', 'desc');
		if(is_numeric($limit)){
			$db->limit($limit, $offset);
		}
		$rtn = $db->get()->result();
		$cache->set($key, $rtn);
		return $rtn;
	}

	/**
	 * 返回目录下文章数量
	 * @param unknown $cat_code
	 * @return boolean|number
	 */
	public function getCount($cat_code){
		$cache = IOSS_Cache::getInstance();
		$key = __METHOD__.'('. implode(',' , func_get_args()) .')';
		if(($datas = $cache->get($key)) !== false){
			return $datas;
		}
		$c = $this->getCategoryInfo($cat_code);
		if(!$c){
			return 0;
		}
		$db = IOSS_DB::getInstance();
		$db->where('category_id', $c->id);
		$db->where('status', self::STATE_ENABLE);
		return $db->count_all_results('cms_node');
	}
	
	/**
	 * 获取目录信息
	 * @param string $cat_code
	 */
	private function getCategoryInfo($code){
		$db = IOSS_DB::getInstance();
		$db->select('cms_model.code as model, cms_category.id as id, cms_category.code as code');
		$db->from('cms_category');
		$db->join('cms_model', 'cms_category.model_id=cms_model.id', 'left');
		$db->where('cms_category.code', $code);
		$row = $db->get()->row();
		return ($row && $row->model) ? $row : null;
	}
	
	private static  function ext($node, $db){
		if($node->model_code == '' || $sub = $db->get_where('cms_node_'.$node->model_code, Array('node_id'=>$node->id))->row() == null){
			return null;
		}
		foreach ($sub as $k=>$v){
			$node->{$k} = $v;
		}
		return $node;
	}
}
